<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="" prop="fromUid">
      <el-input v-model="dataForm.fromUid" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="" prop="toUid">
      <el-input v-model="dataForm.toUid" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="0：回复，1:@我，2:收到赞，3：系统通知，4：私信" prop="type">
      <el-input v-model="dataForm.type" placeholder="0：回复，1:@我，2:收到赞，3：系统通知，4：私信"></el-input>
    </el-form-item>
    <el-form-item label="是否已读:0:false,1:true" prop="hasRead">
      <el-input v-model="dataForm.hasRead" placeholder="是否已读:0:false,1:true"></el-input>
    </el-form-item>
    <el-form-item label="html格式" prop="content">
      <el-input v-model="dataForm.content" placeholder="html格式"></el-input>
    </el-form-item>
    <el-form-item label="" prop="sendTime">
      <el-input v-model="dataForm.sendTime" placeholder=""></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          id: 0,
          fromUid: '',
          toUid: '',
          type: '',
          hasRead: '',
          content: '',
          sendTime: ''
        },
        dataRule: {
          fromUid: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          toUid: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          type: [
            { required: true, message: '0：回复，1:@我，2:收到赞，3：系统通知，4：私信不能为空', trigger: 'blur' }
          ],
          hasRead: [
            { required: true, message: '是否已读:0:false,1:true不能为空', trigger: 'blur' }
          ],
          content: [
            { required: true, message: 'html格式不能为空', trigger: 'blur' }
          ],
          sendTime: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/common/usermessage/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.fromUid = data.userMessage.fromUid
                this.dataForm.toUid = data.userMessage.toUid
                this.dataForm.type = data.userMessage.type
                this.dataForm.hasRead = data.userMessage.hasRead
                this.dataForm.content = data.userMessage.content
                this.dataForm.sendTime = data.userMessage.sendTime
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/common/usermessage/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'fromUid': this.dataForm.fromUid,
                'toUid': this.dataForm.toUid,
                'type': this.dataForm.type,
                'hasRead': this.dataForm.hasRead,
                'content': this.dataForm.content,
                'sendTime': this.dataForm.sendTime
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
